import query from './atom/db/query';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	layout_no: string
}

async function atom(msg: Message) {
	const systemid = '5981';
	console.log(msg);
	const q1 = query(action_id, session_id, spaceid, systemid);
	//依据“编排编号”查<考场编排>，得到“考试科目编号”“考试科目名称”“考场时段编号”；
	q1.prepare('arrange_exam_room', ['_id', 'exam_subjects_id', 'exam_subjects_name', 'exam_date_id'], msg, 1, 1);
	//依据“编排编号”查<学生编排>得到“座位号”，其他字段值为空,以<学生编排>表中的“顺序号”正序排列
	q1.prepare('arrange_exam_student', ['_id', 'seat_no', 'student_id', 'student_no', 'student_type', 'student_name'], msg, 0, 1, ['fzno']);
	const [arrange_exam_rooms, arrange_exam_students] = await q1.exec();
	const arrange_exam_room: any = arrange_exam_rooms[0];
	console.log(arrange_exam_room, '--<1>--', arrange_exam_students, '****');

	const q2 = query(action_id, session_id, spaceid, systemid);
	//依据“考场时段编号”查<考场时段>，得到“考场编号”“房间号”
	q2.prepare('exam_room_date', ['house_number', 'exam_room_id'], { exam_date_id: arrange_exam_room.exam_date_id }, 1, 1);
	//依据“考试科目编号”查<考试科目>得到“教学班编号”“教学班名称”；
	q2.prepare('exam_subjects', ['class_id', 'class_name'], { exam_subjects_id: arrange_exam_room.exam_subjects_id }, 1, 1);
	const [exam_room_dates, exam_subjectss] = await q2.exec();
	const exam_room_date: any = exam_room_dates[0];
	const exam_subjects: any = exam_subjectss[0];
	console.log(exam_room_date, '--<2>--', exam_subjects, '****');

	const q3 = query(action_id, session_id, spaceid, systemid);
	//依据“考场编号”查询<考场>，得到“分布图”“图片地址”
	q3.prepare('exam_room', ['scattergram', 'pic_path'], { exam_room_id: exam_room_date.exam_room_id }, 1, 1);
	const [exam_rooms] = await q3.exec();
	const exam_room = exam_rooms[0];
	console.log(exam_room, '--<3>--');

	const res: any = Object.assign(arrange_exam_room, exam_room_date, exam_subjects, exam_room);
	res.arrange_exam_student = arrange_exam_students;
	console.log('res-->', res);
	return res;
}
